// pages/index/index.js
const app = getApp()
import {
  getDays
} from '../../utils/dateTime.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    getDays,
    isPay: 0,
  },

  toLogin(shareId, agentId) {
    console.log("进入login方法了，");
    console.log(app.globalData.isLogin);
    console.log(shareId);
    console.log(agentId);
    if (!app.globalData.isLogin) {
      ks.showLoading({
        title: '',
        icon: 'none',
      })
      console.log('进login');
      ks.login({
        success: res => {
          console.log(res);

          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          var that = this;
          var url = app.globalData.url + "nine/getOpenId";
          app.wxRequest(url, {
            code: res.code,
            shareId: shareId,
            flag: 1, //  0 微信  1 快手
          }, res => {
            console.log("实现执行这里吗");
            console.log(res);
            if (res.flag) {
              var userInfo = res.data.user;
              app.globalData.openId = res.data.openId;
              app.globalData.userId = res.data.userId;
              app.globalData.isLogin = 1;
              ks.setStorageSync('isLogin', 1);
              ks.setStorageSync('openId', res.data.openId);
              ks.setStorageSync('userId', res.data.userId);
              app.globalData.userInfo = userInfo;
              ks.setStorageSync('userInfo', JSON.stringify(res.data.user));
              if (shareId != 0) {
                app.globalData.shareId = shareId;
                ks.setStorageSync('shareId', shareId);
              }
              that.setData({
                userInfo: userInfo,
                isPay: res.data.flag
              })
              console.log(app.globalData.userType);
              ks.hideLoading();
            }
          }, err => {
            console.log("error");
          })
        }
      })
    } else {
      var userInfo = app.globalData.userInfo;
      this.setData({
        userInfo: app.globalData.userInfo,
      })

      if (userInfo.payStatus == 1) {
        console.log('已支付');
        this.getIndex();
      }

      if (shareId != 0 && userInfo.shareId == 0 && userInfo.payStatus == 0) {
        this.addShare(shareId, agentId);
      }
    }
  },
  getIndex() {
    ks.showLoading({
      title: '加载中...',
    })
    var that = this;
    console.log(this.data.userInfo);
    var url = app.globalData.url + "nine/getIndex";
    app.wxRequest(url, {
      userId: this.data.userInfo.userId
    }, res => {
      console.log(res);
      ks.hideLoading();
      if (res.flag) {
        // 用户身份
        if (res.data.nineUserType != null) {
          app.globalData.userType = res.data.nineUserType.userType;
        }
        console.log(app.globalData);
        that.setData({
          isPay: res.data.flag
        })
      }
    }, err => {

    })
  },

  toPage(e) {
    var type = e.currentTarget.dataset.type
    console.log(this.data.isPay);
    if (this.data.isPay || app.globalData.isPay) {
    // if (true || app.globalData.isPay) {

      if (type == 1) {
        ks.navigateTo({
          url: '/packageA/pages/testPage/testPage',
        })
      } else if (type == 2) {
        ks.navigateTo({
          url: '/packageA/pages/testOtherPage/testOtherPage',
        })
      } else {
        ks.navigateTo({
          url: '/packageC/pages/lifeNumber/lifeNumber',
        })
      }

    } else {
      ks.navigateTo({
        url: '/packageA/pages/buyNow/buyNow',
      })
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {


    console.log(app.globalData.userId);

    console.log("进到index");
    console.log(app.globalData.userInfo);
    console.log(options);
    console.log(Object.keys(options));
    var shareId = 0,
      agentId = 0;
    if (Object.keys(options).length > 0) {
      shareId = options.scene;
      agentId = options.agentId
    }


    console.log(shareId, agentId);
    this.toLogin(shareId, agentId);
  },
  //添加分享人信息
  addShare(shareId, agentId) {
    ks.showLoading({
      title: '加载中...',
    })
    var url = app.globalData.url + "nine/addShare";
    app.wxRequest(url, {
      userId: app.globalData.userId,
      shareId: shareId,
      agentId: agentId
    }, res => {
      if (shareId != 0) {
        app.globalData.shareId = shareId;
        ks.setStorageSync('shareId', shareId)
      }
      ks.hideLoading();
    }, err => { })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        // 当前页面的 tabBar 索引
        selected: 0
      })
    }
    console.log(12);
    console.log(app.globalData.userInfo);
    this.setData({
      isPay: app.globalData.userInfo.payStatus
    })
    console.log(this.data.isPay);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log(1);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  toCon(e) {
    var flag = e.currentTarget.dataset.flag
    console.log(flag);
    if (flag == 'xy') {
      ks.navigateTo({
        url: '/packageB/pages/appXy/appXy',
      })
    } else {
      ks.navigateTo({
        url: '/packageB/pages/appYs/appYs',
      })
    }
  },


})